﻿using System;
using System.ServiceModel;

namespace NLite.Messaging
{
    /// <summary>
    /// 
    /// </summary>
    [AttributeUsage(
                   AttributeTargets.Method
                  , AllowMultiple = false
                  , Inherited = true)]
    public class SubscribeAttribute : Attribute
    {
        /// <summary>
        /// 得到或设置订阅模式
        /// </summary>
        public SubscribeMode Mode { get; set; }

        /// <summary>
        /// 得到或设置消息主题
        /// </summary>
        public string Topic { get; set; }
    }

    /// <summary>
    /// 消息订阅模式
    /// </summary>
    [Flags]
    public enum SubscribeMode
    {
        /// <summary>
        /// 同步订阅
        /// </summary>
        Sync,

        /// <summary>
        /// 异步订阅
        /// </summary>
        Async,
    }

    /// <summary>
    /// 消息总线接口
    /// </summary>
    [Contract]
    [ServiceContract(CallbackContract = typeof(IObserverHandler<IMessage>))]
    [ServiceKnownType(typeof(Message))]
    public interface IMessageBus : IPublisher,IDisposable
    {
       
        /// <summary>
        /// 订阅主题消息
        /// </summary>
        /// <param name="topic">消息主题</param>
        /// <param name="handler">处理器</param>
        /// <returns></returns>
        [OperationContract]
        IDisposable Subscribe(string topic,Type messageType, SubscribeMode mode,IObserverHandler<IMessage> handler);
        
        /// <summary>
        /// 注销订阅的主题消息
        /// </summary>
        /// <param name="topic">消息主题</param>
        /// <param name="handler">处理器</param>
        [OperationContract]
        void Unsubscribe(string topic, Type messageType, IObserverHandler<IMessage> handler);

        /// <summary>
        /// 注册消息监听器
        /// </summary>
        /// <param name="listners">监听器集合</param>
        [OperationContract]
        void RegisterListner(params IMessageListener[] listners);
        /// <summary>
        /// 注销消息监听器
        /// </summary>
        /// <param name="listners">监听器集合</param>
        [OperationContract]
        void UnRegisterListner(params IMessageListener[] listners);

      
        /// <summary>
        /// 移除主题消息
        /// </summary>
        /// <param name="topic">消息主题</param>
        /// <param name="type">类型</param>
        [OperationContract]
        void Remove(string topic, Type type);
        
        /// <summary>
        /// 移除所有主题消息
        /// </summary>
        [OperationContract]
        void RemoveAll();
    }
}